package cn.tedu.hongbei.pms.controller;

import cn.tedu.commons.web.JsonResult;
import cn.tedu.hongbei.pms.pojo.param.CategoryAddNewParam;
import cn.tedu.hongbei.pms.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.hongbei.pms.pojo.vo.CategoryStandardVO;
import cn.tedu.hongbei.pms.pojo.vo.CategoryTreeItemVO;
import cn.tedu.hongbei.pms.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category/")
@Api(tags = "3.类别管理")
@Slf4j
//我们添加此注解是为了检查参数是否符合，下面我们的方法参数中对象中添加Valid和Validated效果相同，
// 但是如果要判断单参数需要使用到validated
@Validated
public class CategoryController {
    @Autowired
    private ICategoryService iCategoryService;

    @PostMapping("add-new")
    @ApiOperation("添加类别")//给此请求加上说明
    @ApiOperationSupport(order = 100)//order增加一个序号来方便排序
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam){
        iCategoryService.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除类别")
    @ApiOperationSupport(order = 200)//order增加一个序号来方便排序
    @PostMapping("deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long", example = "1"),
    })
    public JsonResult deleteById(@Range(min = 1, message = "根据ID删除类别失败，请提交合法的ID值！") @RequestParam Long id){
        iCategoryService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改类别")
    @ApiOperationSupport(order = 300)//order增加一个序号来方便排序
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long"),
    })
    @PostMapping("updateById")
    public JsonResult updateById(@Range(min = 1, message = "根据ID修改类别失败，请提交合法的ID值！") @RequestParam Long id, @Valid CategoryUpdateInfoParam categoryUpdateInfoParam){
        iCategoryService.updateById(id, categoryUpdateInfoParam);
        return JsonResult.ok();
    }

    @ApiOperation("查询类别列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级ID", required = true, dataType = "long", example = "1"),
    })
    public JsonResult list(@Range(min = 0, message = "根据父级ID查找类别失败，请提交合法的父级ID值！") @RequestParam Long parentId){

        return JsonResult.ok(iCategoryService.list(parentId));
    }

    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)//order增加一个序号来方便排序
    @GetMapping("getStandardById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long", example = "1"),
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询相册详情失败，请提交合法的ID值！")
                                      @RequestParam Long id){
        CategoryStandardVO result = iCategoryService.getStandardById(id);
        return JsonResult.ok(result);
    }

    // http://localhost:9080/categories/tree
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryTreeItemVO>> listTree() {
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<CategoryTreeItemVO> categoryTree = iCategoryService.listTree();
        return JsonResult.ok(categoryTree);
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.info("开始处理【启用类别】的请求，参数：{}", id);
        iCategoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/9527/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.info("开始处理【禁用类别】的请求，参数：{}", id);
        iCategoryService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/9527/display
    @PostMapping("/{id:[0-9]+}/display")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisplay(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.info("开始处理【显示类别】的请求，参数：{}", id);
        iCategoryService.setDisplay(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/9527/hidden
    @PostMapping("/{id:[0-9]+}/hidden")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("隐藏类别")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setHidden(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.info("开始处理【隐藏类别】的请求，参数：{}", id);
        iCategoryService.setHidden(id);
        return JsonResult.ok();
    }

}
